// Copyright 2010 Google Inc. All Rights Reserved.

package com.google.zahra.webmanager;

import com.google.zahra.Master;

import com.tcclient.cluster.DsoNode;

import com.google.zahra.testmanager.TestManager;
import com.google.zahra.util.*;
import com.google.zahra.workmanager.ZahraWorkManager;

import org.terracotta.workmanager.pipemanager.Pipe;
import org.terracotta.workmanager.pipemanager.PipeManager;
import org.terracotta.workmanager.routing.RoutableWorkItem;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author arthurkok@google.com (Your Name Here)
 *
 */
public class MainServlet extends HttpServlet{

  static final ZahraWorkManager workManager = WebManager.WORK_MANAGER;
  static final PipeManager<String> pipeManager = WebManager.PIPE_MANAGER;
  static final TestManager testManager = WebManager.TEST_MANAGER;
  
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
      response.setContentType("text/html");
      response.setStatus(HttpServletResponse.SC_OK);

      StringBuffer bodyString = new StringBuffer();

      bodyString.append("<a href='/allTest'>See all Tests</a><br/>");
      bodyString.append("<a href='/allTest?clear=table'>Clear Table</a><br/>");
      bodyString.append("<a href='/allTest?clear=pending'>Clear Pending Tests</a><br/>");
      bodyString.append("<a href='/submitTest'>Submit a new Tests</a><br/>");
      bodyString.append("<a href='/workManager'>Work Manager Control Panel</a><br/>");

      bodyString.append("<h2>Pending Tests</h2>");
      bodyString.append("<table>");
      
      for (int os : Master.listOfOs) {
        bodyString.append("<tr><th width='150'>" + Util.getOsName(os) + "</th><td>" + testManager.getNumberOfPendingTests(os)+ "</td></tr>");
      }
      
      bodyString.append("</table>");
      
      // Connected Workers
      bodyString.append("<h2>Connected Workers [" + workManager.getWorkerNodes().size() +"]</h2>");
      bodyString.append("<table>");
      bodyString.append(
          String.format("<table border='1'><tr><th>%s</th><th>%s</th>"
              + "<th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>",
              "ID",
              "Host Name",
              "IP",
              "OS",
              "Status",
              "Pending Tests"));

      for (DsoNode node : workManager.getWorkerNodes()) {
        Pipe<RoutableWorkItem<String>, String> pipe = pipeManager.get(node.getId());

        if (pipe != null) {

          String output =
              "<tr><td><a href='/workerInfo?workerId=" + node.getId() + "'>" + node.getId()
                  + "</a></td>";
          output += "<td>" + node.getHostname() + "</td>";
          output += "<td>" + node.getIp() + "</td>";
          output += "<td>" + Util.getOsName(pipeManager.getWorkerOs(node.getId())) + "</td>";
          output += "<td><table class='clear'><tr><td width='75'>" + pipe.getStatus() + "</td><td>";
          
          if (pipe.getStatus().equals("RUNNING")) {          
            output += "<div style='height:10; width:10; background-color:green'></div>";
          }
          else if (pipe.getStatus().equals("STOPPED")) {
            output += "<div style='height:10; width:10; background-color:red'></div>";
          }
          else {
            output += "<div style='height:10; width:10; background-color:orange'></div>";
          }
          output += "</td></tr></table></td>";
          output += "<td>" + pipe.getPendingWorkQueue().size() + "</td></tr>";

          bodyString.append(output);
        }
      }
      bodyString.append("</table>");
      response.getWriter().print(Util.getHTML(bodyString.toString(), "Zahra - Home"));
    }
  
}
